﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DepartList.aspx.cs" Inherits="Saker.Web.Boss.Sys.Depart.DepartList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="../../../Styles/global.css?v=1.0" rel="stylesheet" type="text/css" />
    <link href="../../../Scripts/zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet"
        type="text/css" />
    <script src="../../../Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
    <script src="../../../Scripts/jquery.utils.js" type="text/javascript"></script>
    <script src="../../../Scripts/admin.js?v=1.0" type="text/javascript"></script>
    <script src="../../../Scripts/admin.ui.js?v=1.0" type="text/javascript"></script>
    <script src="../../../Scripts/zTree/js/jquery.ztree.all-3.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        admin.config.indexUrl = '/Boss/Main.aspx';
        var loading_img = "../../../images/loading_16x16.gif";

        // 只调用最顶的jBox
        if (top.jBox != undefined) {
            window.jBox = top.jBox;
        }

        top.orderChanged = false;
        function checkReload() {
            if (top.orderChanged) {
                top.orderChanged = false;
                goUrl(location.href);
            }
        }
        function goUrl(url) {
            top.admin.ui.goUrl(url);
            // 可能在jBox里点击跳转，同时关闭它
            jBox.close();
        }
        /* 树视图 */
        function initTreeView(treeId, dataUrl) {
            var setting = {
                key: {
                    checked: "checked",
                    childs: "childs",
                    name: "name",
                    title: "name"
                },
                view: {
                    expandSpeed: "",
                    selectedMulti: true
                },
                async: {
                    enable: true,
                    type: "post",
                    dataType: 'json',
                    autoParam: ["id=id"],
                    url: dataUrl
                },
                callback: {
                    onAsyncError: function (event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
                        jBox.tip('加载树失败！', 'error');
                    }
                }
            };

            var tree = $("#" + treeId);
            $.fn.zTree.init(tree, setting);
        }
        // 新增根节点
        function doAddRootNode() {
            doAddNode(null, '添加根节点', '');
        }
        // 新增子节点
        function doAddChildNode() {
            var zTree = doGetTree();
            var nodes = zTree.getSelectedNodes();
            if (nodes.length != 1) {
                jBox.tip('请选择上级部门！', 'warning');
                return;
            }
            var pNode = nodes[0];
            doAddNode(pNode, '为[' + pNode.name + ']添加子部门', pNode.id);
        }
        function doAddNode(node, title, parentId) {
            var html =
                '<table class="data-form" style="margin:10px;" cellspacing="0" cellpadding="0">' +
                    '<tbody>' +
                        '<tr>' +
                            '<th scope="row">部门名称：</th>' +
                            '<td><input type="text" id="name" name="name" class="input-normal" /></td>' +
                        '</tr>' +
                    '</tbody>' +
                '</table>';
            var submit = function (v, h, f) {
                if (f.name == '') {
                    jBox.tip("请输入部门名称。", 'error', { focusId: "name" });
                    return false;
                }
                // 提交数据
                jBox.tip('正在处理...', 'loading');
                $.postJson('DepartAdd.ashx?CMD=add', { parentId: parentId, name: f.name }, function (data) {
                    if (data.code != 1) {
                        jBox.tip(data.message, 'error');
                    }
                    else {
                        jBox.close();
                        jBox.tip(data.message, 'success');
                        ////////
                        node.isParent = true;
                        node.iconOpen = "/images/tree/server_group_open.png";
                        node.iconClose = "/images/tree/server_group_close.png";
                        /////
                        doRefresh(node);
                    }
                }, function () {
                    jBox.tip("服务器出错！", 'error');
                });

                return false;
            };

            jBox.open(html, title, 500, 'auto', { submit: submit });
        }
        // 编辑节点
        function doEditNode() {
            var zTree = doGetTree();
            var nodes = zTree.getSelectedNodes();
            if (nodes.length != 1) {
                jBox.tip('请选择一个部门！', 'warning');
                return;
            }
            var pNode = nodes[0];
            var html = "<div style='padding:10px;'>部门名称：<input type='text' id='name' name='name' class='input-normal' value='" + pNode.name + "' /></div>";
            var submit = function (v, h, f) {
                if (v == 'ok') {
                    if (f.name == '') {
                        jBox.tip("请输入部门名称。", 'error', { focusId: "name" });
                        return false;
                    }
                    jBox.tip('正在处理...', 'loading');
                    $.postJson('DepartAdd.ashx?CMD=edit', { id: pNode.id, name: f.name }, function (data) {
                        if (data.code != 1) {
                            jBox.tip(data.message, 'error');
                        }
                        else {
                            jBox.close();
                            jBox.tip(data.message, 'success');
                            doRefresh(pNode.getParentNode());
                        }
                    }, function () {
                        jBox.tip("服务器出错！", 'error');
                    });
                }
                return false;
            };

            jBox(html, { title: "编辑部门", submit: submit });
        }
        // 删除部门
        function doDeleteNodes() {
            var zTree = doGetTree();
            var nodes = zTree.getSelectedNodes();
            if (nodes.length == 0) {
                jBox.tip('请选择至少一个部门！', 'warning');
                return;
            }
            // 如果只删除一个节点，则只刷新父节点数据
            var parentNode = nodes.length == 1 ? nodes[0].getParentNode() : null;
            // 获取要删除的数据
            var ids = $.map(nodes, function (n) { return n.id; });

            jBox.confirm("确定要删除选中的部门吗？", "删除部门", function (v, h, f) {
                if (v == 'ok') {
                    jBox.tip('正在处理...', 'loading');
                    $.postJson("DepartAdd.ashx?CMD=del", { ids: ids.join(",") }, function (data) {
                        if (data.code != 1) {
                            jBox.tip(data.message, 'error');
                        }
                        else {
                            jBox.tip(data.message, 'success');
                            doRefresh(parentNode);
                        }
                    }, function () {
                        jBox.tip("服务器出错！", 'error');
                    });
                }
                return true;
            });
        }
        function doGetTree() {
            return $.fn.zTree.getZTreeObj('tree-view-depart');
        }
        function doRefresh(node) {
            var zTree = doGetTree();
            zTree.cancelSelectedNode(); // 刷新前需要取消已选择数据
            zTree.reAsyncChildNodes(node, "refresh");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="block-wp">
        <div class="block">
            <div class="h">
                <span class="icon-sprite icon-list"></span>
                <h3>
                    部门列表</h3>
                <div class="bar">
                    <a class="btn-lit" href="javascript:doRefresh();"><span>刷新</span></a> | <a class="btn-lit"
                        href="javascript:doAddChildNode();"><span>新增子部门</span></a> <a class="btn-lit" href="javascript:doSetChildOrder();">
                            <span>排序子部门</span></a> | <a class="btn-lit" href="javascript:doEditNode();"><span>编辑部门</span></a>
                    <a class="btn-lit" href="javascript:doDeleteNodes();"><span>删除已选</span></a>
                </div>
            </div>
            <div class="tl corner">
            </div>
            <div class="tr corner">
            </div>
            <div class="bl corner">
            </div>
            <div class="br corner">
            </div>
            <div class="cnt-wp">
                <div class="cnt">
                    <table class="data-table" width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tbody>
                            <tr class="odd">
                                <td>
                                    <ul id="tree-view-depart" class="ztree">
                                    </ul>
                                    <script type="text/javascript">
                                        initTreeView('tree-view-depart', '/Boss/Sys/Depart/DepartHandler.ashx');
                                    </script>
                                </td>
                            </tr>
                            <tr>
                                <td class="txt-gray" style="text-indent: 10px; border-top: 1px solid #DDDDDD;">
                                    说明：按住 Ctrl 键可以同时选择多个节点。
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <!--#wp-->
    </form>
</body>
</html>
